package com.ja.openchat.viewmodels;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.google.gson.JsonObject;
import com.ja.openchat.ui.me.PersonalInformationFragment;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.discover.ChatNearbyInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.LocationHelper;

import java.util.Map;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class NearbyItemViewModel extends MultiItemViewModel<NearbyViewModel> {

    public ObservableField<Integer> gender = new ObservableField<>(0);
    public ObservableField<String> name = new ObservableField<>("");
    public ObservableField<String> distance = new ObservableField<>("");

    public ObservableField<UserInfo> userAvatar = new ObservableField<>();
    private ChatNearbyInfo entity;

    public NearbyItemViewModel(@NonNull NearbyViewModel viewModel, ChatNearbyInfo entity) {
        super(viewModel);
        this.entity = entity;
        requestUserInfo(entity.chat_id);
        distance.set(LocationHelper.distanceFormat(entity.distance));
    }


    private UserInfo requestUserInfo(long userId) {
        if (viewModel.userInfoHashMap.containsKey(userId)) {
            userAvatar.set(viewModel.userInfoHashMap.get(userId));
            userAvatar.notifyChange();
            name.set(viewModel.userInfoHashMap.get(userId).displayName());
            return viewModel.userInfoHashMap.get(userId);
        } else {
            TelegramManager.getTelegramManager().requestContactInfo(viewModel.getApplication(), userId, new TgResultBlock<UserInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, UserInfo obj) {
                    updateUserInfo(obj);
                }

                @Override
                public void onTimeout(Map<String, Object> request) {

                }
            });
            return null;
        }
    }

    public void updateUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        name.set(userInfo.displayName());
        userAvatar.set(userInfo);
        userAvatar.notifyChange();
        viewModel.userInfoHashMap.put(userInfo.id, userInfo);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            getData();
            Bundle bundle = new Bundle();
            bundle.putString("distance", distance.get());
            bundle.putLong("userId", userAvatar.get().id);
            viewModel.startContainerActivity(PersonalInformationFragment.class.getCanonicalName(), bundle);
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
        }
    });

//    void getData() {
//        ChatNearbyInfo nearbyInfo = entity;
//        viewModel.showProgress();
//        TelegramManager.getTelegramManager().createPrivateChat(viewModel.getApplication(), nearbyInfo.chat_id, new TgResultBlock<ChatInfo>() {
//            @Override
//            public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
//                viewModel.hideProgress();
//                if (obj != null) {
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("conversation", obj);
//                    viewModel.startActivity(ChatActivity.class, bundle);
//                } else {
//                    viewModel.showErrorTip("请求错误，请稍后重试");
//                }
//            }
//
//            @Override
//            public void onTimeout(Map<String, Object> request) {
//                viewModel.hideProgress();
//                viewModel.showErrorTip("请求超时，请稍后重试");
//            }
//        });
//    }
}
